package dongze.com.ckdj.ui;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;

/**
 * Created by fanqwz on 2017/11/15.
 */

public class ListInScroller extends ListView {
    private float lastY;

    public ListInScroller(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            requestDisallowInterceptTouchEvent(true);
        } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            if (lastY > ev.getY()) {
                // 如果是向上滑动，且不能滑动了，则让ScrollView处理
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    if (!canScrollList(1)) {
                        requestDisallowInterceptTouchEvent(false);
                        return false;
                    } else if (ev.getY() > lastY) {
                        // 如果是向下滑动，且不能滑动了，则让ScrollView处理
                        if (!canScrollList(-1)) {
                            requestDisallowInterceptTouchEvent(false);
                            return false;
                        }
                    }
                } else {
                    requestDisallowInterceptTouchEvent(true);
                }
            }
        }
        lastY = ev.getY();
        return super.dispatchTouchEvent(ev);
    }
}
